<template>
  <el-dialog :title="type === 'add' ? '新增账目' : '编辑账目'"
    :visible="dialogVisible"
    width="680px"
    @close="close">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-row>
        <el-col :span="12">
          <el-form-item label="金额" prop="money">
            <el-input-number v-model="ruleForm.money"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="支付方式" prop="pay">
            <el-select v-model="ruleForm.pay" placeholder="请选择支付方式" style="width:100%;">
              <el-option label="支付宝" value="支付宝"></el-option>
              <el-option label="微信钱包" value="微信钱包"></el-option>
              <el-option label="银行卡" value="银行卡"></el-option>
              <el-option label="信用卡" value="信用卡"></el-option>
              <el-option label="花呗" value="花呗"></el-option>
              <el-option label="京东白条" value="京东白条"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="支付时间" prop="ptime">
            <el-date-picker
              v-model="ruleForm.ptime"
              type="datetime"
              placeholder="选择支付时间">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="位置" prop="location">
            <el-cascader
              v-model="ruleForm.location"
              ref="cascader"
              :options="options"
              @change="handleChange">
            </el-cascader>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="类别" prop="type">
            <el-select v-model="ruleForm.type" placeholder="请选择类别" style="width:100%;" @change="changeType(ruleForm.type)">
              <el-option label="收入" :value="1"></el-option>
              <el-option label="支出" :value="0"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="分类" prop="tag">
            <el-select
              v-model="ruleForm.tag"
              placeholder="请选择">
              <el-option
                v-for="item in typeOptions"
                :key="item.id"
                :label="item.name"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input type="textarea" v-model="ruleForm.remark"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="reset('ruleForm')">取 消</el-button>
      <el-button v-if="type === 'add'" type="primary" @click="save('ruleForm')" :loading="loading">确 定</el-button>
      <el-button v-if="type === 'edit'" type="primary" @click="update('ruleForm')" :loading="loading">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
let pcas = require('../../../assets/pca-code.json') // 省市区数据
import moment from 'moment'
export default {
  data () {
    return {
      location: {},
      ruleForm: {
        money: '',
        type: 1,
        tag: '',
        pay: '',
        ptime: '',
        location: '',
        remark: ''
      },
      rules: {
        money: [
          { required: true, message: '请输入账目金额', trigger: 'blur' }
        ],
        pay: [
          { required: true, message: '请输入支付方式', trigger: 'blur' }
        ]
      },
      loading: false,
      type: 'add',
      typeOptions: [],
      options: pcas
    }
  },
  props: {
    dialogVisible: {
      type: Boolean
    },
    list: {
      type: Array
    }
  },
  created () {
  },
  methods: {
    // 获取省市区地址级联
    handleChange(e, form, thsAreaCode) {
      thsAreaCode = this.$refs['cascader'].getCheckedNodes()[0].pathLabels// 注意2： 获取label值
      this.location = { code: e, city: thsAreaCode }
    },
    close () {
      this.$refs.ruleForm.resetFields()
      this.location = {}
      this.$emit('update:dialogVisible', false)
    },
    open () {
      this.type = 'add'
      this.getList(1)
      this.$emit('update:dialogVisible', true)
    },
    save(formName) { // 新增
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true
          const params = {
            money: this.ruleForm.money,
            type: this.ruleForm.type,
            tag: this.ruleForm.tag,
            location: this.location,
            pay: this.ruleForm.pay,
            uid: this.$Cookies.get('uid'),
            ptime: moment(this.ruleForm.ptime).format('YYYY-MM-DD HH:mm:ss'),
            remark: this.ruleForm.remark
          }
          this.$API({
            name: 'AddAccount',
            data: params,
            requireAuth: true
          }).then(res => {
            if (res.data.code === 200) {
              this.$message.success(res.data.msg)
              this.close()
              this.$emit('getAll')
            } else {
              this.$message.error(res.data.msg)
            }
          }).catch(error => {
            this.$message.error(error)
          }).finally(() => {
            this.loading = false
            this.location = {}
          })
        }
      })
    },
    reset(formName) {
      this.location = {}
      this.$refs[formName].resetFields()
      this.$emit('update:dialogVisible', false)
    },
    detaliInfo (id) { // 根据id获取详情
      this.type = 'edit'
      this.$emit('update:dialogVisible', true)
      this.$API({
        name: 'DetailAccount',
        paths: [id],
        requireAuth: true
      }).then (res => {
        if (res.data.code === 200) {
          this.getList(res.data.data.type)
          this.ruleForm = res.data.data
          this.location = res.data.data.location
          this.ruleForm.location = res.data.data.location.code
        } else {
          this.$message.error(res.data.msg)
        }
      }).catch(error => {
        this.$message.error(error)
      })
    },
    update(formName) { // 编辑
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true
          const params = {
            id: this.ruleForm.id,
            money: this.ruleForm.money,
            type: this.ruleForm.type,
            tag: this.ruleForm.tag,
            location: this.location,
            pay: this.ruleForm.pay,
            uid: this.$Cookies.get('uid'),
            ptime: moment(this.ruleForm.ptime).format('YYYY-MM-DD HH:mm:ss'),
            remark: this.ruleForm.remark
          }
          this.$API({
            name: 'UpdateAccount',
            data: params,
            requireAuth: true
          }).then(res => {
            if (res.data.code === 200 && res.data.data[0] === 1) {
              this.$message.success(res.data.msg)
              this.close()
              this.$emit('getAll')
            } else {
              this.$message.error(res.data.msg)
              this.close()
            }
          }).catch(error => {
            this.$message.error(error)
          }).finally(() => {
            this.loading = false
          })
        }
      })
    },
    changeType (type) {
      this.ruleForm.tag = ''
      this.typeOptions = []
      this.getList(type)
    },
    getList (type) { // 获取支出/收入列表
      this.$API({
        name: 'GetAccountTagList',
        params: {
          page: 1,
          size: 1000,
          type: type
        },
        requireAuth: true
      }).then (res => {
        if (res.data.code === 200) {
          this.typeOptions = res.data.data.data
          this.typeOptions.forEach(item => {
            this.$set(item, 'value', `{ "name": "${item.name}", "icon": "${item.icon}" }`)
          })
        } else {
          this.$message.error(res.data.msg)
        }
      }).catch(error => {
        this.$message.error(error)
      }).finally(() => {
      })
    },
  }
}
</script>

<style>
/* 解决级联选择器高度溢出和底部滚动条的问题 */
.el-cascader-panel {
  height: 204px;
}
.el-cascader-panel .el-scrollbar__wrap{
  overflow-x: hidden;
}

</style>